/**
 * Copyright 2007 Christian Mueller
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy of 
 * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 
 * applicable law or agreed to in writing, software distributed under the License 
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * Description:
 * This file contains functions handling replicids of databases!
 */

#include "myglobal.h"
#include "globopts.h"
#include "replicid.h"
#include <nsfdb.h>
#include <ostime.h>

void run_get_replic_id()
{
 	LOG_DEBUG1("void run_get_replic_id() - start");
 	
 	/** Check Database **/
	LOG_DEBUG2("check database entry");
	if (opt_database[0]=='\0') {
		LOG_ERROR("Please define the database with option --database <string>");
		return;	
	}
	
	execute_get_replic_id(opt_database);
	 	
 	LOG_DEBUG1("void run_get_replic_id() - end");
}
 
void run_create_replic_id()
{
 	LOG_DEBUG1("void run_create_replic_id() - start");
 	
 	/** Check Database **/
	LOG_DEBUG2("check database entry");
	if (opt_database[0]=='\0') {
		LOG_ERROR("Please define the database with option --database <string>");
		return;	
	}
	
	execute_create_replic_id(opt_database);
	 	
 	LOG_DEBUG1("void run_create_replic_id() - end");
}

void run_copy_replic_id()
{
 	LOG_DEBUG1("void run_copy_replic_id() - start");
 	
 	/** Check Database **/
 	LOG_DEBUG2("check source-database entry");
	if (opt_source_database[0]=='\0') {
		LOG_ERROR("Please define the database with option --source-database <string>");
		return;	
	}
	
	LOG_DEBUG2("check dest-database entry");
	if (opt_database[0]=='\0') {
		LOG_ERROR("Please define the database with option --dest-database <string>");
		return;	
	}
	
	execute_copy_replic_id(opt_source_database,opt_database);
	 	
 	LOG_DEBUG1("void run_create_replic_id() - end");
}

void execute_get_replic_id(const char *databasename)
{
 	LOG_DEBUG1("void execute_get_replic_id(databasename) - start");
 	if (HAS_DEBUG2) {
		char debug_string[256];
		sprintf(debug_string,"databasename = %s\n",databasename);
		LOG_DEBUG2(debug_string);
	}
	
	// Open the database
	LOG_DEBUG2("Trying to open the database....");
	LOG_DEBUG3("call NSFDbOpen (path_name, &db_handle)");
	DBHANDLE    db_handle;
	if (NSFDbOpen (databasename, &db_handle)) {
		LOG_ERROR("The defined database couldn't be opend!");
		return;
	}	
	
	
	// Get the database info
	DBREPLICAINFO replica_info;
	LOG_DEBUG3("call NSFDbReplicaInfoGet(db_handle,&replica_info)");
	if (NSFDbReplicaInfoGet(db_handle,&replica_info))
    {
        LOG_ERROR("Replicainfo couldn't be retrieved!");
        NSFDbClose (db_handle);
        return;
    }
    
    char string[512];
    sprintf(string,"Database Replica ID: %#010lx - %#010lx\n", 
    	replica_info.ID.Innards[1], replica_info.ID.Innards[0] );
	
	LOG_INFO(string);
	
	// Closing database
	LOG_DEBUG3("call NSFDbClose (db_handle)");
	if (NSFDbClose (db_handle))
   	{
       LOG_ERROR("The database handle couldn't be closed!");
       return;
   	}
	
	
 	LOG_DEBUG1("void execute_get_replic_id(databasename) - end");
}

void execute_create_replic_id(const char *databasename)
{
 	LOG_DEBUG1("void execute_create_replic_id(databasename) - start");
 	if (HAS_DEBUG2) {
		char debug_string[256];
		sprintf(debug_string,"databasename = %s\n",databasename);
		LOG_DEBUG2(debug_string);
	}
	
	// Open the database
	LOG_DEBUG2("Trying to open the database....");
	LOG_DEBUG3("call NSFDbOpen (path_name, &db_handle)");
	DBHANDLE    db_handle;
	if (NSFDbOpen (databasename, &db_handle)) {
		LOG_ERROR("The defined database couldn't be opend!");
		return;
	}	
	
	
	// Get the database info
	DBREPLICAINFO replica_info;
	LOG_DEBUG3("call NSFDbReplicaInfoGet(db_handle,&replica_info)");
	if (NSFDbReplicaInfoGet(db_handle,&replica_info))
    {
        LOG_ERROR("Replicainfo couldn't be retrieved!");
        NSFDbClose (db_handle);
        return;
    }
    
    char string[512];
    sprintf(string,"The old Database Replica ID: %#010lx - %#010lx\n", 
    	replica_info.ID.Innards[1], replica_info.ID.Innards[0] );
	LOG_INFO(string);
	
	TIMEDATE newTime;
	LOG_DEBUG3("call OSCurrentTIMEDATE(&newTime)");
	OSCurrentTIMEDATE(&newTime);
	
	replica_info.ID = newTime;
	
	sprintf(string,"The new Database Replica ID: %#010lx - %#010lx\n", 
    	replica_info.ID.Innards[1], replica_info.ID.Innards[0] );
	LOG_INFO(string);
	
	LOG_DEBUG3("call NSFDbReplicaInfoSet(db_handle,&replica_info)");
	if (NSFDbReplicaInfoSet(db_handle,&replica_info))
    {
        LOG_ERROR("Replicainfo couldn't be set!");
        NSFDbClose (db_handle);
        return;
    }
    
	// Closing database
	LOG_DEBUG3("call NSFDbClose (db_handle)");
	if (NSFDbClose (db_handle))
   	{
       LOG_ERROR("The database handle couldn't be closed!");
       return;
   	}
	
	
 	LOG_DEBUG1("void execute_create_replic_id(databasename) - end");
}

void execute_copy_replic_id(const char *sourceDb, const char *destDb)
{
	LOG_DEBUG1("void execute_copy_replic_id(databasename) - start");
	if (HAS_DEBUG2) {
		char debug_string[256];
		sprintf(debug_string,"source-databasename = %s\n",sourceDb);
		LOG_DEBUG2(debug_string);
		sprintf(debug_string,"dest-databasename = %s\n",destDb);
		LOG_DEBUG2(debug_string);
	}
	
	// Open the source database
	LOG_DEBUG2("Trying to open the source database....");
	DBHANDLE db_source;
	LOG_DEBUG3("call NSFDbOpen (sourceDb, &db_source)");
	if (NSFDbOpen (sourceDb, &db_source)) {
		LOG_ERROR("The defined source database couldn't be opend!");
		return;
	}	
	
	// Open the dest database
	LOG_DEBUG2("Trying to open the dest database....");
	
	DBHANDLE db_dest;
	LOG_DEBUG3("call NSFDbOpen (destDb, &db_dest)");
	if (NSFDbOpen (destDb, &db_dest)) {
		LOG_ERROR("The defined source database couldn't be opend!");
		NSFDbClose (db_source);
		return;
	}	
	
	DBREPLICAINFO source_replica_info;
	DBREPLICAINFO dest_replica_info;
	
	LOG_DEBUG3("call NSFDbReplicaInfoGet(sourceDb,&source_replica_info)");
	if (NSFDbReplicaInfoGet(db_source,&source_replica_info))
    {
        LOG_ERROR("Replicainfo couldn't be retrieved!");
        NSFDbClose (db_source);
        NSFDbClose (db_dest);
        return;
    }
    
    LOG_DEBUG3("call NSFDbReplicaInfoGet(db_dest,&dest_replica_info)");
	if (NSFDbReplicaInfoGet(db_dest,&dest_replica_info))
    {
        LOG_ERROR("Replicainfo couldn't be retrieved!");
        NSFDbClose (db_source);
        NSFDbClose (db_dest);
        return;
    }
	
	char string[512];
    sprintf(string,"The Database %s gets Replica ID: %#010lx - %#010lx\n", 
    	destDb,
    	source_replica_info.ID.Innards[1], source_replica_info.ID.Innards[0] );
	LOG_INFO(string);
	
	dest_replica_info.ID = source_replica_info.ID;
	
	LOG_DEBUG3("call NSFDbReplicaInfoSet(db_dest,&dest_replica_info)");
	if (NSFDbReplicaInfoSet(db_dest,&dest_replica_info))
    {
        LOG_ERROR("Replicainfo couldn't be set!");
        NSFDbClose (db_source);
        NSFDbClose (db_dest);
        return;
    }
    
	// Close Databases
	LOG_DEBUG3("call NSFDbClose (db_source)");
	NSFDbClose (db_source);
	LOG_DEBUG3("call NSFDbClose (db_dest)");
	NSFDbClose (db_dest);
	
	
	LOG_DEBUG1("void execute_copy_replic_id(databasename) - end");
}

